package com.ny.demo527;

import java.sql.*;
import java.util.Scanner;

public class TestConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/usersdb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             Scanner scanner = new Scanner(System.in)) {

            Class.forName("com.mysql.cj.jdbc.Driver");

            String sql = "INSERT INTO users(username, password) VALUES (?, ?)";
            try (PreparedStatement pstm = conn.prepareStatement(sql)) {
                System.out.println("请输入用户名");
                String username = scanner.next();
                System.out.println("请输入密码");
                String password = scanner.next();

                pstm.setString(1, username);
                pstm.setString(2, password);

                int i = pstm.executeUpdate();
                if (i > 0) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("注册失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
